package development.bean;

import state.GameConstants;
import state.GameImage;
import state.Sprite;
import state.util.ImageUtil;
import development.Constants;
import development.game.FrmBroadChess;

public class BroadChessMan extends Sprite {
  public static final int TOT_TYPE = 0;

  public static final int XE_TYPE = 1;

  public static final int MA_TYPE = 2;

  public static final int TUONG_TYPE = 3;

  public static final int HAU_TYPE = 4;

  public static final int VUA_TYPE = 5;

  public static final int CHOOSE_AREA_TYPE = 7;

  public static final int RED_SIDE = 0;

  public static final int BLACK_SIDE = 1;

  public static final int NO_SIDE = 2;

  public static final int INFINITE = 99;

  private int column;
  private int row;
  private int type;
  private int side;
  private int[][] availableMove;

  public BroadChessMan(int side1, int type1, GameImage image, int column1, int row1) {
    super(image, null, FrmBroadChess.BOARD_DX + FrmBroadChess.CELL_SIZE * column1, FrmBroadChess.BOARD_DY + FrmBroadChess.CELL_SIZE * row1, GameConstants.CENTER_ANCHOR);
    this.type = type1;
    this.column = column1;
    this.row = row1;
    this.side = side1;

    switch (type1) {
    case TOT_TYPE:
      if (side1 == RED_SIDE) {
        availableMove = new int[][] { { 0, -1 }, { 0, -2 }, null, null };
      } else {
        availableMove = new int[][] { { 0, 1 }, { 0, 2 }, null, null };
      }
      break;
    case XE_TYPE:
      availableMove = new int[][] { { INFINITE, 0 } };
      break;
    case MA_TYPE:
      availableMove = new int[][] { { -1, 2 }, { 1, 2 }, { -1, -2 }, { 1, -2 }, { 2, -1 }, { 2, 1 }, { -2, -1 }, { -1, 2 },
          { -2, 1 } };
      break;
    case TUONG_TYPE:
      availableMove = new int[][] { { INFINITE, INFINITE } };
      break;
    case HAU_TYPE:
      availableMove = new int[][] { { INFINITE, 0 }, { INFINITE, INFINITE } };
      break;
    case VUA_TYPE:
      availableMove = new int[][] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 }, { 1, -1 }, { 1, 1 }, { -1, -1 }, { -1, 1 } };
      break;
    }
  }

  public int[][] getAvailableMove() {
    if (availableMove == null) {
      return null;
    }

    int[][] array = new int[availableMove.length][];
    for (int i = 0; i < availableMove.length; i++) {
      if (availableMove[i] != null) {
        array[i] = new int[2];
        for (int j = 0; j < availableMove[i].length; j++) {
          array[i][j] = availableMove[i][j];
        }
      }
    }
    return array;
  }

  public Sprite move(int dColumn, int dRow) {
    column += dColumn;
    row += dRow;

    if (type == TOT_TYPE) {
      checkPassRiver();
    }
    setPosition(FrmBroadChess.BOARD_DX + FrmBroadChess.CELL_SIZE * column, FrmBroadChess.BOARD_DY + FrmBroadChess.CELL_SIZE * row);
    return this;
  }

  public void changePosition(int column1, int row1) {
    this.column = column1;
    this.row = row1;

    if (type == TOT_TYPE) {
      checkPassRiver();
    }
    setPosition(FrmBroadChess.BOARD_DX + FrmBroadChess.CELL_SIZE * column1, FrmBroadChess.BOARD_DY + FrmBroadChess.CELL_SIZE * row1);
  }

  private void checkPassRiver() {
    availableMove[1] = null;

    if (side == RED_SIDE) {
      if (row == 0) {
        type = HAU_TYPE;
        availableMove = new int[][] { { INFINITE, 0 }, { INFINITE, INFINITE } };
        _image = ImageUtil.getImage(Constants.CO_VUA_IMAGE_CATEGORY, "CoVua05.png");
      }
      if (row == 7) {
        _image = ImageUtil.getImage(Constants.CO_VUA_IMAGE_CATEGORY, "CoVua05.png");
      }
    } else {
      if (row == 0) {
        _image = ImageUtil.getImage(Constants.CO_VUA_IMAGE_CATEGORY, "CoVua15.png");
      }
      if (row == 7) {
        type = HAU_TYPE;
        availableMove = new int[][] { { INFINITE, 0 }, { INFINITE, INFINITE } };
        _image = ImageUtil.getImage(Constants.CO_VUA_IMAGE_CATEGORY, "CoVua15.png");
      }
    }
  }

  public int getColumn() {
    return column;
  }

  public int getRow() {
    return row;
  }

  public int getType() {
    return type;
  }

  public int getSide() {
    return side;
  }
}
